package com.sens.common;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class ProcessHooker extends BaseHooker {
    public ProcessHooker() {
        super("*");
    }

    private void printAllView(ViewGroup viewGroup) {
        Log.i("viewgroup--->" + viewGroup);
        if (viewGroup == null) return;
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View view = viewGroup.getChildAt(i);
            if (view instanceof ViewGroup) {
                printAllView((ViewGroup) view);
            } else if (view instanceof TextView) {
                Log.i("textView--->" + view + " text:" + ((TextView) view).getText());
            } else {
                Log.i("View--->" + view);
            }
        }
    }

    @Override
    public void handlePackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        if (lpparam.isFirstApplication)
            XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, attachHook);
    }

    class AlipayActivityLifecycleCallbacks extends ActivityLifecycleCallback {

        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            Log.d("ProcessHooker:onActivityCreated--->" + activity + " savedInstanceState:" + savedInstanceState);
            if (activity.getClass().getName().equals("com.alipay.mobile.nebulacore.ui.H5Activity")) {
                Intent intent = activity.getIntent();
                Log.i("intent:" + intent + " Extras:" + intent.getExtras());
            }
        }

        @Override
        public void onActivityStarted(Activity activity) {
            Log.d("ProcessHooker:onActivityStarted--->" + activity);
            if (activity.getClass().getName().equals("com.alipay.mobile.nebulacore.ui.H5Activity")) {
                printAllView((ViewGroup) activity.getWindow().getDecorView());
            }
        }

        @Override
        public void onActivityResumed(Activity activity) {
            Log.d("ProcessHooker:onActivityResumed--->" + activity);
        }

        @Override
        public void onActivityPaused(Activity activity) {
            Log.d("ProcessHooker:onActivityPaused--->" + activity);
        }

        @Override
        public void onActivityStopped(Activity activity) {
            Log.d("ProcessHooker:onActivityStopped--->" + activity);
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            Log.d("ProcessHooker:onActivitySaveInstanceState--->" + activity + " outState:" + outState);
        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            Log.d("ProcessHooker:onActivityDestroyed--->" + activity);
        }
    }

    private AlipayActivityLifecycleCallbacks lifecycleCallbacks = new AlipayActivityLifecycleCallbacks();
    private XC_MethodHook attachHook = new XC_MethodHook() {
        @Override
        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
            super.afterHookedMethod(param);
            Log.i("ProcessHooker--->" + param.thisObject.toString());
            ((Application) param.thisObject).registerActivityLifecycleCallbacks(lifecycleCallbacks);
        }
    };
}
